package org.datascooter.extension;

import java.io.IOException;

import org.datascooter.DataScooter;
import org.datascooter.impl.DataSource;
import org.datascooter.impl.DummyDataSourceProvider;
import org.datascooter.inface.IBundleProvider;
import org.datascooter.inface.ISnipBuilderProvider;
import org.datascooter.inface.ISnipManager;
import org.datascooter.parser.DSBuilderProvider;
import org.datascooter.parser.DSConnectorProvider;
import org.datascooter.parser.DSDataSourceProvider;
import org.datascooter.parser.DSMappingProvider;

public class DatascooterStarter {

	/**
	 * The simple zero-starter - let's start H2 in desired directory - so you data will be stored there and you will be able to control it with H2 console
	 * 
	 * @param userDir
	 */
	public static void startH2DefaultPath(String userDir) {
		DataScooter.startZero(getDefaultDir(), userDir, "sa", "");
	}

	/**
	 * start datascooter in eclipse application assume all required files (datascooter.properties, datasource.properties,plugin.xml) exists in the root of product plugin
	 * 
	 * @throws IOException
	 */
	public static void startEclipseProduct() throws IOException {
		DataScooter.start(EclipsePathResolver.resolvePath("datascooter.properties"), new DSBuilderProvider(new EclipseExtensionReader("org.datascooter.clauseBuilder")), new DSConnectorProvider(
			new EclipseExtensionReader("org.datascooter.connector")), new DSDataSourceProvider(new EclipseExtensionReader("org.datascooter.dataSource")), new DSMappingProvider(
			new EclipseExtensionReader("org.datascooter.mapping")));
	}

	/**
	 * Use DataScooter.startZero(String propertiesPath, String url, String user, String password) - with default path - 'System.getProperty("user.dir")' - 'propertiesPath' may be null so will be used
	 * default properties, if 'url' do not start with 'jdbc:' but just simple directory path - so will be used H2 data base. If 'url' starts with 'jdbc:' - so will be loaded JDBC-driver for required
	 * database (be sure if required driver exists and in classpath)
	 */
	public static void startZeroDefaultPath() {
		DataScooter.startZero(getDefaultDir());
	}

	/**
	 * start datascooter in eclipse application assume all required files (datascooter.properties, datasource.properties,plugin.xml) exists in the System.getProperty("user.dir")
	 * 
	 * @throws IOException
	 */
	public static void startEclipseDefaultPath() {
		DataScooter.start(getDefaultDir() + "/datascooter.properties", new DSBuilderProvider(new EclipseExtensionReader("org.datascooter.clauseBuilder")), new DSConnectorProvider(
			new EclipseExtensionReader("org.datascooter.connector")), new DSDataSourceProvider(new EclipseExtensionReader("org.datascooter.dataSource")), new DSMappingProvider(
			new EclipseExtensionReader("org.datascooter.mapping")));
	}

	public static void startRemote(ISnipManager manager, ISnipBuilderProvider builderProvider, IBundleProvider... bundleProviders) {
		DataScooter.startRemote(manager, getDefaultDir(), builderProvider, bundleProviders);
	}

	public static void startRemoteEclipse(ISnipManager manager) {
		DataScooter.startRemote(manager, getDefaultDir(), new DSBuilderProvider(new EclipseExtensionReader("org.datascooter.clauseBuilder")), new DSMappingProvider(new EclipseExtensionReader(
			"org.datascooter.mapping")));
	}

	public static void startEclipseDBSettings(String path, String url, String user, String password) {
		DataScooter.start(
			path,
			new DSBuilderProvider(new EclipseExtensionReader("org.datascooter.clauseBuilder")),
			new DSConnectorProvider(new EclipseExtensionReader("org.datascooter.connector")),
			new DummyDataSourceProvider(new DataSource(url, url, user, password, true)),
			new DSMappingProvider(new EclipseExtensionReader("org.datascooter.mapping")));
	}

	public static void startEclipse(String path) {
		DataScooter.start(
			path,
			new DSBuilderProvider(new EclipseExtensionReader("org.datascooter.clauseBuilder")),
			new DSConnectorProvider(new EclipseExtensionReader("org.datascooter.connector")),
			new DSDataSourceProvider(new EclipseExtensionReader("org.datascooter.dataSource")),
			new DSMappingProvider(new EclipseExtensionReader("org.datascooter.mapping")));
	}

	private static String getDefaultDirParent() {
		return System.getProperty("user.dir").replace(".test", "");
	}

	private static String getDefaultDir() {
		return System.getProperty("user.dir");
	}
}
